//
//  YAYIApilyListController.m
//  YAYIDoctor
//
//  Created by hxp on 16/5/4.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYIApilyListController.h"
#import "YAYIApliyDetailController.h"
#import "YAYIMYPatientModel.h"
#import "YAYIApilyListCell.h"
#import "YAYISearchBar.h"

#import "YAYIHospitalDropListView.h"

@interface YAYIApilyListController ()<UITableViewDelegate, UITableViewDataSource, YAYIHospitalDropListViewDelegate>

@property (nonatomic,strong)YAYISearchBar *searchBar;
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic, weak) UILabel *noDataFlagLbl;

@property (nonatomic,strong)NSMutableArray *dataAry;
@property (nonatomic,copy)NSString *hospital;
@property (nonatomic,copy)NSString *hospitalid;

/**
 *  dropList
 */
@property (nonatomic, weak) YAYIHospitalDropListView *dropMenu;
@property (nonatomic, strong) NSMutableArray *hospitalArray;
@property (nonatomic, copy) NSString *hospitalID;

@end


@implementation YAYIApilyListController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.dataAry = [NSMutableArray array];
    
    [self createSearchBar];
//    [self currentHospital];
    [self createTableView];
    
    // 注意：下拉列表必须放在最后初始化
    [self setupDropMenu];
    
    [self getHospitalList];
}


- (void)setupDropMenu
{
    //上边按钮展示的模型,此模型对text赋值即可.
    self.hospitalArray = [NSMutableArray array];
    
    //若列表展示内容与按钮展示内容相同则showArr传nil即可。
    YAYIHospitalDropListView *dropMenu = [[YAYIHospitalDropListView alloc] initWithFrame:CGRectMake(70, 0, SCREEN_width - 70 * 2, 36) showOnView:self.view AllDataArr:self.hospitalArray showArr:@"选择医院"];
    dropMenu.delegate = self;
    self.dropMenu = dropMenu;
    self.navigationItem.titleView = dropMenu;
}


-(void)createSearchBar{
    
    if (self.searchBar == nil) {
        self.searchBar = [YAYISearchBar new];
        self.searchBar.placeholder = @"搜索医院、医生";
        self.searchBar.barTintColor = [UIColor whiteColor];
        self.searchBar.layer.borderWidth = 1;
        self.searchBar.layer.borderColor = [UIColor whiteColor].CGColor;
        [self.view addSubview:self.searchBar];
            }
    [self.searchBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.right.equalTo(@(0));
        make.top.equalTo(@(0));
        make.size.mas_equalTo(CGSizeMake(SCREEN_width, 56));
    }];
    
}


-(void)createTableView{
    if (self.tableView == nil) {
        self.tableView = [UITableView new];
        self.tableView.delegate =self;
        self.tableView.dataSource =self;
        self.tableView.tableFooterView = [UIView new];
        self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.view addSubview:self.tableView];
    }
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(0));
        make.top.mas_equalTo(64);
        make.size.mas_equalTo(CGSizeMake(SCREEN_width, SCREEN_height));
    }];
    
    // 下拉刷新
    __block typeof(self) weakself = self;
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        
        if (self.hospitalID) {
            [weakself getCharegeByHospitalid:self.hospitalID];
        }else{
            [weakself.tableView.mj_header endRefreshing];
        }
    }];
    
}



/**
 *  没有数据时显示
 */
- (void)showNoDataFlag:(BOOL)flag
{
    if (!flag) {
        [self.noDataFlagLbl removeFromSuperview];
        self.noDataFlagLbl = nil;
        return;
    }
    
    if (self.noDataFlagLbl) {
        return;
    }
    UILabel *flagLbl = [[UILabel alloc] init];
    flagLbl.text = @"暂时没有收费";
    flagLbl.font = [UIFont systemFontOfSize:15];
    flagLbl.textColor = YAYIColor(107, 178, 242);
    flagLbl.textAlignment = NSTextAlignmentCenter;
    [self.tableView addSubview:flagLbl];
    self.noDataFlagLbl = flagLbl;
    [flagLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.tableView);
        make.centerY.equalTo(self.tableView).offset(-50);
    }];
}


#pragma mark - 获取数据
/**
 *  获取医院列表
 */
- (void)getHospitalList
{
    if (self.hospitalArray.count > 0) {
        return;
    }
    
    [YAYINetWorking getWithURL:curHospital_url param:nil finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        if (error == nil) {
            YAYILog(@"返回:%@", dict);
            
            if (dict != nil) {
                
                if ([dict[@"data"] isEqual:NULL]) {
                    return ;
                }else{
                    
                    NSArray *data = dict[@"data"];
                    for (NSDictionary *obj in data) {
                        YAYIHosiptalModel *model = [[YAYIHosiptalModel alloc] init];
                        model.name = obj[@"name"];
                        model.hospitalid = obj[@"hospitalid"];
                        [self.hospitalArray addObject:model];
                        
                        YAYILog(@"model.id:%@ model.name:%@", obj[@"wpid"], model.name);
                        
                    }
                    
                    if (self.hospitalArray.count > 0) {
                        
                        [self.dropMenu refreshData:self.hospitalArray];
                        [self.dropMenu selectRow:0];
                        
                        // 刷新列表
                        YAYIHosiptalModel *hostModel = self.hospitalArray[0];
                        self.hospitalID = hostModel.hospitalid;
                        self.hospital = hostModel.name;
                        [self getCharegeByHospitalid:hostModel.hospitalid];
                    }else{
                    
                        [self showNoDataFlag:YES];
                    }
                }
                
            }else{
                YAYILog(@"网络异常");
            }
        }
        
    }];
}


-(void)getCharegeByHospitalid:(NSString *)hospitalid
{

    [self.dataAry removeAllObjects];
    
    NSMutableDictionary *param = [NSMutableDictionary dictionary];
    param[@"hospitalid"] = hospitalid;
    
    [YAYINetWorking  getWithURL:charegeuser_url param:param finish:^(NSData *data, NSDictionary *dict, NSError *error) {
        NSString *status = dict[@"status"];
        YAYILog(@"%@",dict);
        if ([status isEqualToString:@"success"]) {
            NSArray *data = dict[@"data"];
            
            for (NSDictionary *obj in data) {
                
                YAYIMYPatientModel *model = [YAYIMYPatientModel new];
                [model setValuesForKeysWithDictionary:obj];
                [self.dataAry addObject:model];
            }
            
            if (self.dataAry.count == 0) {
                [self showNoDataFlag:YES];
            }else{
                [self showNoDataFlag:NO];
            }
            
            [self.tableView reloadData];
        }
        
        [self.tableView.mj_header endRefreshing];
    }];
}


- (void)refreshPage
{
    
    if (self.hospitalID) {
        [self getCharegeByHospitalid:self.hospitalID];
    }
}


#pragma mark - 点击事件


#pragma mark - 代理方法
-(UIView *)tableViewHeaderView{
    
    UIView *headerView = [UIView  new];
    headerView.frame = CGRectMake(0, 0, SCREEN_width, 36);
    headerView.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
    
    UILabel *title = [UILabel new];
    title.textColor = [UIColor grayColor];
    title.font = [UIFont systemFontOfSize:font14];
    title.frame = CGRectMake(20, 0, 100, 36);
    title.text = @"我的患者";
    [headerView addSubview:title];
    return headerView;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataAry.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
   static NSString *identifer = @"indetifer";
    YAYIApilyListCell *cell = [tableView  dequeueReusableCellWithIdentifier:identifer];
    if (cell == nil) {
        cell = [[YAYIApilyListCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifer];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.model = self.dataAry[indexPath.row];
    return cell;

}


-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
   
    return [self tableViewHeaderView];
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 36;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    YAYIApliyDetailController *apilyView = [YAYIApliyDetailController new];
    YAYIMYPatientModel *model = self.dataAry[indexPath.row];
    
    apilyView.title = @"收费详情";
    apilyView.avatar = model.avatar;
    apilyView.clientname = model.clientname;
    apilyView.patientname = model.patientname;
    apilyView.hospital = self.hospital;
    apilyView.hospitalid = self.hospitalID;
    apilyView.bookingid = [self.dataAry[indexPath.row] bookingid];
    apilyView.userid = [self.dataAry[indexPath.row] userid];
    apilyView.mobile = model.mobile;
    [self.navigationController pushViewController:apilyView animated:YES];
}


#pragma mark YAYIDropMenuView代理
- (void)selectColum:(NSInteger)colum
{
    YAYILog(@"选择了低%ld列", colum);
    //    [self getHospitalList];
}

- (void)selectRow:(NSInteger)row Model:(YAYIHosiptalModel *)model
{
    YAYILog(@"选择了第%ld行, %@ id:%@", row, model.name, model.hospitalid);
    
    // 先清除数据
    self.hospitalID = model.hospitalid;
    self.hospital = model.name;
    [self getCharegeByHospitalid:model.hospitalid];
}


@end
